/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	var form = $("#customSignup");
        var billfname = $("#billFirstName");
	var billfnameInfo = $("#billFirstNameInfo");
        var shipfname = $("#shipFirstName");
	var shipfnameInfo = $("#shipFirstNameInfo");
	var billlname = $("#billLastName");
	var billlnameInfo = $("#billLastNameInfo");
        var shiplname = $("#shipLastName");
	var shiplnameInfo = $("#shipLastNameInfo");
	var email = $("#email");
	var emailInfo = $("#emailInfo");
	var pass1 = $("#pass1");
	var pass1Info = $("#pass1Info");
	var pass2 = $("#pass2");
	var pass2Info = $("#pass2Info");
	var billStreet1 = $("#billStreet1");
        var billStreet1Info = $("#billStreet1Info");
        var shipStreet1 = $("#shipStreet1");
        var shipStreet1Info = $("#shipStreet1Info");
        var billPhone = $("#billPhone");
        var billPhoneInfo = $("#billPhoneInfo");
        var shipPhone = $("#shipPhone");
        var shipPhoneInfo = $("#shipPhoneInfo");
        var billState = $("#billState");
        var billStateInfo = $("#billStateInfo");
        var shipState = $("#shipState");
        var shipStateInfo = $("#shipStateInfo");
        var billPostalcode = $("#billPostalcode");
        var billPostalcodeInfo = $("#billPostalcodeInfo");
        var shipPostalcode = $("#shipPostalcode");
        var shipPostalcodeInfo = $("#shipPostalcodeInfo");
        var billCity = $("#billCity");
        var billCityInfo = $("#billCityInfo");
	var shipCity = $("#shipCity");
        var shipCityInfo = $("#shipCityInfo");
	//On blur
        /*
        username.blur(validateFirstName);
	name.blur(validateLastName);
	email.blur(validateEmail);
	pass1.blur(validatePass1);
	pass2.blur(validatePass2);
        billPhone.blur(validatePhone);
	billStreet1.blur(validateAddress);
        state.blur(validateState);        
        postalcode.blur(validatePostalcode);
        city.blur(validateCity);
        */
              
	//On Submitting
	form.submit(function(){                
		if(dupplicateAddress() & validateFirstName() & validateFirstName2() & validateLastName() & validateLastName2() & validateEmail() & validatePass1() & validatePass2() 
                        & validatePhone() & validatePhone2() & validateAddress() & validateAddress2() & validateCity() & validateCity2() 
                            & validateState() & validatePostalcode() & validateState2() & validatePostalcode2())
			return true;
		else
			return false;
	});
	
	//validation functions
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("error");
			emailInfo.text("");
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			emailInfo.text("Invalid e-mail address");
			emailInfo.addClass("error");
			return false;
		}
	}
	function validateLastName(){
		//if it's NOT valid
		if(billlname.val().length < 3){
			billlname.addClass("error");
			billlnameInfo.text("Last Name must has more than 3 letters");
			billlnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			billlname.removeClass("error");
			billlnameInfo.text("");
			billlnameInfo.removeClass("error");
			return true;
		}                                
	}
        
        function validateLastName2(){
            if(shiplname.val().length < 3){
			shiplname.addClass("error");
			shiplnameInfo.text("Last Name must has more than 3 letters");
			shiplnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			shiplname.removeClass("error");
			shiplnameInfo.text("");
			shiplnameInfo.removeClass("error");
			return true;
		}
        }
        function validateFirstName2(){
            //validate shipping
            if(shipfname.val().length < 3){
                    shipfname.addClass("error");
                    shipfnameInfo.text("First name must has at least 3 letters");
                    shipfnameInfo.addClass("error");
                    return false;
            }
            //if it's valid
            else{
                    shipfname.removeClass("error");
                    shipfnameInfo.text("");
                    shipfnameInfo.removeClass("error");
                    return true;
            }
        }
        function validateFirstName(){
            //if it's NOT valid
            if(billfname.val().length < 3){
                    billfname.addClass("error");
                    billfnameInfo.text("First name must has at least 3 letters");
                    billfnameInfo.addClass("error");
                    return false;
            }
            //if it's valid
            else{
                    billfname.removeClass("error");
                    billfnameInfo.text("");
                    billfnameInfo.removeClass("error");
                    return true;
            }                                
	}
        function validatePhone2(){
            //validate shipping phone
            if(shipPhone.val().length < 10){
                    shipPhone.addClass("error");
                    shipPhoneInfo.text("Phone number must has more than 9 numbers");
                    shipPhoneInfo.addClass("error");
                    return false;
            }
            //if it's valid
            else if(isNaN(shipPhone.val())){
                    shipPhone.addClass("error");
                    shipPhoneInfo.text("Must be numbers");
                    shipPhoneInfo.addClass("error");
                    return false;
            }
            else{
                    shipPhone.removeClass("error");
                    shipPhoneInfo.text("");
                    shipPhoneInfo.removeClass("error");
                    return true;
            }
        }
	function validatePhone(){
            //if it's NOT valid
            if(billPhone.val().length < 10){
                    billPhone.addClass("error");
                    billPhoneInfo.text("Phone number must has more than 9 numbers");
                    billPhoneInfo.addClass("error");
                    return false;
            }
            //if it's valid
            else if(isNaN(billPhone.val())){
                    billPhone.addClass("error");
                    billPhoneInfo.text("Must be numbers");
                    billPhoneInfo.addClass("error");
                    return false;
            }
            else{
                    billPhone.removeClass("error");
                    billPhoneInfo.text("");
                    billPhoneInfo.removeClass("error");
                    return true;
            }                                
	}
        
	function validatePass1(){		

		//it's NOT valid
		if(pass1.val().length < 6){
			pass1.addClass("error");
			pass1Info.text("At least 6 characters: letters, numbers and '_'");
			pass1Info.addClass("error");
			return false;
		}
		//it's valid
		else{			
			pass1.removeClass("error");
			pass1Info.text("");
			pass1Info.removeClass("error");
			validatePass2();
			return true;
		}
	}
	function validatePass2(){		
		//are NOT valid
		if( pass1.val() != pass2.val() ){
			pass2.addClass("error");
			pass2Info.text("Passwords doesn't match!");
			pass2Info.addClass("error");
			return false;
		}
		//are valid
		else{
			pass2.removeClass("error");
			pass2Info.text("");
			pass2Info.removeClass("error");
			return true;
		}
	}
        
        function validateAddress2(){
            if(shipStreet1.val().length < 1){
                    shipStreet1.addClass("error");
                    shipStreet1Info.addClass("error");
                    shipStreet1Info.text("Street address is required");
                    return false;
            }
            //it's valid
            else{			
                    shipStreet1.removeClass("error");
                    shipStreet1Info.text("");
                    shipStreet1Info.removeClass("error");
                    return true;
            }
        }
        
	function validateAddress(){
            //it's NOT valid
            if(billStreet1.val().length < 1){
                    billStreet1.addClass("error");
                    billStreet1Info.addClass("error");
                    billStreet1Info.text("Street address is required");
                    return false;
            }
            //it's valid
            else{			
                    billStreet1.removeClass("error");
                    billStreet1Info.text("");
                    billStreet1Info.removeClass("error");
                    return true;
            }                                
	}
        
        function validateState2(){
            //validate shipping state
            if(shipState.val().length < 1){
                shipState.addClass("error");
                shipStateInfo.addClass("error");
                shipStateInfo.text("Please select your state");
                return false;
            }
            //it's valid
            else{			
                shipState.removeClass("error");                        
                shipStateInfo.text("");
                return true;
            }
        }
        
	function validateState(){
            //it's NOT valid
            if(billState.val().length < 1){
                billState.addClass("error");
                billStateInfo.addClass("error");
                billStateInfo.text("Please select your state");
                return false;
            }
            //it's valid
            else{			
                billState.removeClass("error");                        
                billStateInfo.text("");
                return true;
            }                               
	}
        function validatePostalcode2(){
            //validate shipping address: zip code
            if(shipPostalcode.val().length < 1){
                    shipPostalcode.addClass("error");
                    shipPostalcodeInfo.text("Postal code is required");
                    shipPostalcodeInfo.addClass("error");
                    return false;
            }
            //if it's valid
            else if(isNaN(shipPostalcode.val())){
                    shipPostalcode.addClass("error");
                    shipPostalcodeInfo.text("Must be numbers");
                    shipPostalcodeInfo.addClass("error");
                    return false;
            }
            else{
                    shipPostalcode.removeClass("error");
                    shipPostalcodeInfo.text("");
                    shipPostalcodeInfo.removeClass("error");
                    return true;
            }
        }
	function validatePostalcode(){
		//if it's NOT valid
		if(billPostalcode.val().length < 1){
                    billPostalcode.addClass("error");
                    billPostalcodeInfo.text("Postal code is required");
                    billPostalcodeInfo.addClass("error");
                    return false;
		}
		//if it's valid
		else if(isNaN(billPostalcode.val())){
                    billPostalcode.addClass("error");
                    billPostalcodeInfo.text("Must be numbers");
                    billPostalcodeInfo.addClass("error");
                    return false;
                }
                else{
                    billPostalcode.removeClass("error");
                    billPostalcodeInfo.text("");
                    billPostalcodeInfo.removeClass("error");
                    return true;
		}                                
	}
        function validateCity2(){
            //validate shpping address 
            if(shipCity.val().length < 1){
                    shipCity.addClass("error");
                    shipCityInfo.addClass("error");
                    shipCityInfo.text("City is required");
                    return false;
            }
            //it's valid
            else{			
                    shipCity.removeClass("error");
                    shipCityInfo.text("");
                    shipCityInfo.removeClass("error");
                    return true;
            }
        }
        
	function validateCity(){
            //it's NOT valid
            if(billCity.val().length < 1){
                    billCity.addClass("error");
                    billCityInfo.addClass("error");
                    billCityInfo.text("City is required");
                    return false;
            }
            //it's valid
            else{			
                    billCity.removeClass("error");
                    billCityInfo.text("");
                    billCityInfo.removeClass("error");
                    return true;
            }                                
	}
        
        
});

function dupplicateAddress(){
    if($("#oneContact").is(':checked')){        
        var billfname = $("#billFirstName");
        $("#shipFirstName").val(billfname.val());
        var billlname = $("#billLastName");
        $("#shipLastName").val(billlname.val());
        var billStreet1 = $("#billStreet1");
        $("#shipStreet1").val(billStreet1.val());
        var billStreet2 = $("#billStreet2");
        $("#shipStreet2").val(billStreet2.val());
        var billPhone = $("#billPhone");
        $("#shipPhone").val(billPhone.val());
        var billState = $("#billState");
        $("#shipState").val(billState.val());
        var billPostalcode = $("#billPostalcode");
        $("#shipPostalcode").val(billPostalcode.val());
        var billCity = $("#billCity");
        $("#shipCity").val(billCity.val());
    }
    
    return true;
}